<template>
    <div class="bed-overview-root">
        <!-- 顶部统计横幅（滚动） -->
        <div class="top-summary-scroll">
            <div class="scrolling-content">
                总床位：{{ stat.totalBeds }}　空闲：<span class="stat-free">{{ stat.freeNum }}</span>　
                外出：<span class="stat-out">{{ stat.outwardNum }}</span>　
                有人：<span class="stat-occupied">{{ stat.occupiedNum }}</span>
            </div>
        </div>

        <!-- 楼层选择和右侧自定义提示按钮 -->
        <div class="floor-select-row">
            <div class="floor-label">当前楼层：</div>
            <div class="floor-btn-group">
                <el-button v-for="f in floors" :key="f" class="floor-btn" :type="currentFloor === f ? 'primary' : 'default'"
                    @click="onFloorChange(f)">
                    {{ f.replace('F', '层') }}
                </el-button>
            </div>
            <div class="status-toggle-icon" :class="{ active: showStatus }" @click="showStatus = !showStatus">
                <svg t="1750650715759" class="icon" viewBox="0 0 1024 1024" version="1.1" xmlns="http://www.w3.org/2000/svg"
                    p-id="30407" width="40px" height="40px">
                    <path d="M551.1 549.6m-306 0a306 306 0 1 0 612 0 306 306 0 1 0-612 0Z" fill="#25B195" p-id="30408">
                    </path>
                    <path
                        d="M515.3 884.8c-49.6 0-97.8-9.7-143.1-28.9-43.8-18.5-83.1-45-116.9-78.8-33.8-33.8-60.3-73.1-78.8-116.9-19.2-45.3-28.9-93.5-28.9-143.1s9.7-97.8 28.9-143.1c18.5-43.8 45-83.1 78.8-116.9 33.8-33.8 73.1-60.3 116.9-78.8 45.3-19.2 93.5-28.9 143.1-28.9s97.8 9.7 143.1 28.9c43.8 18.5 83.1 45 116.9 78.8 33.8 33.8 60.3 73.1 78.8 116.9 19.2 45.2 28.9 93.4 28.9 143s-9.7 97.8-28.9 143.1c-18.5 43.8-45 83.1-78.8 116.9-33.8 33.8-73.1 60.3-116.9 78.8-45.3 19.2-93.5 29-143.1 29z m0-694.5c-180.2 0-326.7 146.6-326.7 326.7s146.6 326.7 326.7 326.7C695.4 843.8 842 697.2 842 517S695.4 190.3 515.3 190.3z"
                        fill="#666767" p-id="30409"></path>
                    <path
                        d="M562.3 633.8C538.8 669 515 696.3 475 696.6c-0.4 0-0.9 0-1.3-0.1-27.1-4.7-38.2-24.3-32.3-44.3l52-172c1.3-4.2-0.8-8.7-4.7-10.1-3.8-1.4-11.4 3.6-17.9 10.7l-19.5 23.4c-4.1 5-12.3 1.9-12.1-4.6v-0.2c0-1.3 0.4-2.6 1.2-3.6 23.5-34.4 61.2-61.3 87-61.7h0.9c24.5 2.8 36.2 22.4 32.1 44.1 0 0.3-0.1 0.5-0.2 0.8l-52 172c-0.1 0.3-0.2 0.7-0.2 1.1-0.4 3.8 1.6 7.4 5 8.6 3.8 1.4 11.9-3.6 18.4-10.8l18.8-22.7c4.2-5 12.4-1.8 12.1 4.7v1.9zM555.3 409.1c-19.9 0-36-14.4-36-35.8 0-21.3 16.1-35.8 36-35.8s36 14.5 36 35.8c0 21.3-16.1 35.8-36 35.8z"
                        fill="#595857" p-id="30410"></path>
                </svg>
            </div>
        </div>

        <!-- 床位卡片区域 -->
        <div class="bed-list-scroll">
            <el-row :gutter="28">
                <el-col v-for="(roomBeds, roomNumber) in groupedBeds" :key="roomNumber" :xs="24" :sm="12" :md="8" :lg="8">
                    <el-card class="room-block-card" shadow="hover">
                        <div class="room-title">
                            <el-icon><i-ep-house /></el-icon>
                            房间 {{ roomNumber }}
                        </div>
                        <div class="bed-list">
                            <div v-for="bed in roomBeds" :key="bed.bedId" class="bed-item" :class="{
                                'bed-status-free': bed.status === 'free',
                                'bed-status-occupied': bed.status === 'occupied',
                                'bed-status-outward': bed.status === 'outward'
                            }" @click="showBedDialog(bed)">
                                <span class="bed-icon">
                                    <el-icon><i-ep-suitcase /></el-icon>
                                </span>
                                <div class="bed-no">床{{ String(bed.bedNumber).padStart(2, '0') }}号</div>
                            </div>
                        </div>
                    </el-card>
                </el-col>
            </el-row>
        </div>

        <!-- 右下角状态说明（可切换显示/隐藏） -->
        <transition name="fade">
            <div v-show="showStatus" class="legend-box">
                <div><span class="dot dot-free"></span>空闲：<span style="color:#19be6b;">绿色</span></div>
                <div><span class="dot dot-occupied"></span>有人：<span style="color:#409EFF;">蓝色</span></div>
                <div><span class="dot dot-outward"></span>外出：<span style="color:#faad14;">橙色</span></div>
            </div>
        </transition>

        <!-- 床位详情弹窗（美观，支持拖拽） -->
        <el-dialog v-model="showDialog" width="340px" title="床位详情" :close-on-click-modal="false" align-center draggable>
            <el-descriptions :column="1" border>
                <el-descriptions-item label="房间号">{{ dialogInfo.roomNo }}</el-descriptions-item>
                <el-descriptions-item label="床位号">{{ dialogInfo.bedNo }}</el-descriptions-item>
                <el-descriptions-item label="状态">
                    <span v-if="dialogInfo.status === 'used'" style="color:#1976d2;">
                        <el-icon class="status-dot">
                            <UserFilled />
                        </el-icon> 有人
                    </span>
                    <span v-else-if="dialogInfo.status === 'free'" style="color:#19be6b;">
                        <el-icon class="status-dot">
                            <Check />
                        </el-icon> 空闲
                    </span>
                    <span v-else style="color:#faad14;">
                        <el-icon class="status-dot">
                            <WarningFilled />
                        </el-icon> 外出
                    </span>
                </el-descriptions-item>
                <el-descriptions-item label="客户姓名">
                    {{ dialogInfo.clientName || '—' }}
                </el-descriptions-item>
                <el-descriptions-item label="入住时间">
                    {{ dialogInfo.checkIn || '—' }}
                </el-descriptions-item>
            </el-descriptions>
        </el-dialog>
    </div>
</template>
  
<script setup>
import {
    House, Suitcase, InfoFilled, UserFilled, Check, WarningFilled
} from '@element-plus/icons-vue'

import { ref, reactive, computed, onMounted, inject } from 'vue'
const axios = inject('axios')
const iEpHouse = House
const iEpSuitcase = Suitcase

const floors = ['1F', '2F', '3F', '4F', '5F', '6F']
const currentFloor = ref('1F')
const stat = reactive({
    totalBeds: 0,
    freeNum: 0,
    outwardNum: 0,
    occupiedNum: 0
})
const beds = ref([])
const showStatus = ref(true)

// 弹窗相关
const showDialog = ref(false)
const dialogInfo = ref({
    roomNo: '',
    bedNo: '',
    status: '',
    clientName: '',
    checkIn: ''
})

async function loadBeds(floor) {
    try {
        const res = await axios.get('/bed/getBedNum', { params: { floorNumber: floor } })
        if (res.data.status === 200 && res.data.data) {
            stat.totalBeds = res.data.data.totalBeds || 0
            stat.freeNum = res.data.data.freeNum || 0
            stat.outwardNum = res.data.data.outwardNum || 0
            stat.occupiedNum = res.data.data.occupiedNum || 0
        } else {
            stat.totalBeds = stat.freeNum = stat.outwardNum = stat.occupiedNum = 0
        }
    } catch (e) {
        stat.totalBeds = stat.freeNum = stat.outwardNum = stat.occupiedNum = 0
    }
    try {
        const res2 = await axios.get('/bed/getList', { params: { floorNumber: floor } })
        if (res2.data.status === 200 && Array.isArray(res2.data.data)) {
            beds.value = res2.data.data
        } else {
            beds.value = []
        }
    } catch (e) {
        beds.value = []
    }
}

const groupedBeds = computed(() => {
    const map = {};
    (beds.value || []).forEach(b => {
        if (!map[b.roomNumber]) map[b.roomNumber] = [];
        map[b.roomNumber].push(b);
    });
    return map;
})

function onFloorChange(f) {
    currentFloor.value = f
    loadBeds(f)
}

// 美观弹窗内容转换
async function showBedDialog(bed) {
    // 获取弹窗内容，可根据实际数据调整映射
    try {
        const res = await axios.get('/bed/getBedInfo', { params: { bedId: bed.bedId } })
        if (res.data.status === 200) {
            const data = res.data.data
            dialogInfo.value = {
                roomNo: data.bed?.roomNumber ?? '-',
                bedNo: String(data.bed?.bedNumber ?? '-').padStart(2, '0'),
                status: data.bed?.status === 'occupied' ? 'used'
                    : data.bed?.status === 'free' ? 'free'
                        : data.bed?.status === 'outward' ? 'out' : '',
                clientName: data.client?.name || '',
                checkIn: data.bedUsage?.startDate || ''
            }
            showDialog.value = true
        }
    } catch (e) {
        dialogInfo.value = {
            roomNo: '-',
            bedNo: '-',
            status: '',
            clientName: '',
            checkIn: ''
        }
        showDialog.value = false
    }
}

onMounted(() => {
    loadBeds(currentFloor.value)
})
</script>
  
<style scoped>
.bed-overview-root {
    background: #f6f9fb;
    width: 100%;
    box-sizing: border-box;
    position: relative;
}

.top-summary-scroll {
    width: 100%;
    max-width: 100%;
    background: linear-gradient(90deg, #e1fbea 80%, #f6f9fb 100%);
    overflow: hidden;
    white-space: nowrap;
    padding: 0.6em 0 0.6em 0;
    position: sticky;
    top: 0;
    z-index: 20;
    box-shadow: 0 1px 7px #c3efe3 inset;
}

.scrolling-content {
    width: 100%;
    min-width: unset;
    font-size: 1.15em;
    font-weight: bold;
    animation: scroll-left 18s linear infinite;
    letter-spacing: 1px;
}

@keyframes scroll-left {
    0% {
        transform: translateX(0);
    }

    100% {
        transform: translateX(-40%);
    }
}

.stat-free {
    color: #19be6b;
    font-weight: 600;
}

.stat-out {
    color: #faad14;
    font-weight: 600;
}

.stat-occupied {
    color: #409EFF;
    font-weight: 600;
}

.floor-select-row {
    display: flex;
    align-items: center;
    font-size: 1.02em;
    padding: 0.8em 1.8em 0.6em 1.3em;
    background: #f6f9fb;
    position: sticky;
    top: 2.5em;
    z-index: 19;
    box-shadow: 0 1px 7px #e5ecee inset;
    border-bottom: 1px solid #e8f0ee;
    min-height: 56px;
}

.floor-label {
    margin-right: 1em;
    font-size: 1.08em;
    color: #1a3036;
    font-weight: 500;
}

.floor-btn-group {
    flex: 1;
}

.floor-btn {
    margin-right: 0.7em;
    min-width: 58px;
    font-weight: 500;
    letter-spacing: 1px;
}

.status-toggle {
    margin-left: auto;
    display: flex;
    align-items: center;
    margin-right: 8px;
}

.bed-list-scroll {
    margin-top: 1.5em;
    width: 100%;
    min-height: 30vh;
    padding: 1.1vw 3vw 1.2vw 3vw;
    box-sizing: border-box;
}

.room-block-card {
    border-radius: 15px !important;
    box-shadow: 0 4px 20px rgba(60, 120, 180, 0.07);
    background: #fff;
    margin-bottom: 2.5em;
    padding: 20px 22px 22px 22px;
    min-height: 150px;
    transition: box-shadow 0.22s;
    border: 1.4px solid #e2ebea;
}

.room-block-card:hover {
    box-shadow: 0 6px 28px rgba(60, 120, 200, 0.16);
}

.room-title {
    font-size: 1.14em;
    font-weight: bold;
    margin-bottom: 16px;
    display: flex;
    align-items: center;
    color: #384b4e;
    letter-spacing: 1px;
}

.room-title .el-icon {
    margin-right: 8px;
}

.bed-list {
    display: flex;
    flex-wrap: wrap;
    gap: 15px;
    width: 100%;
    justify-content: flex-start;
}

.bed-item {
    cursor: pointer;
    text-align: center;
    border-radius: 9px;
    padding: 13px 0 9px 0;
    min-width: 74px;
    flex: 1 0 28%;
    border: 1.5px solid #f2f2f2;
    background: #f7fafb;
    transition: box-shadow .18s, background .18s, border .15s;
    display: flex;
    flex-direction: column;
    align-items: center;
    min-height: 76px;
}

.bed-item:hover {
    box-shadow: 0 2px 16px #e6e6e6;
    background: #fffbe7;
    border-color: #b6e7da;
}

.bed-icon {
    font-size: 2em;
    margin-bottom: 2px;
    display: block;
}

.bed-status-free {
    color: #19be6b;
}

.bed-status-occupied {
    color: #409EFF;
}

.bed-status-outward {
    color: #faad14;
}

.bed-no {
    font-size: 1.06em;
    color: #444;
    margin-top: 3px;
    font-weight: 500;
}

.legend-box {
    position: fixed;
    right: 2vw;
    bottom: 3vh;
    background: #fff;
    border-radius: 14px;
    box-shadow: 0 2px 14px rgba(60, 60, 100, 0.10);
    padding: 1.4em 1.1em;
    font-size: 1em;
    line-height: 2.1;
    min-width: 125px;
    letter-spacing: 1px;
    border: 1px solid #e1e6e9;
    opacity: 0.98;
    z-index: 99;
    transition: opacity 0.2s, box-shadow 0.2s;
}

.dot {
    display: inline-block;
    width: 0.9em;
    height: 0.9em;
    border-radius: 50%;
    margin-right: 8px;
    vertical-align: middle;
}

.dot-free {
    background: #19be6b;
}

.dot-occupied {
    background: #409EFF;
}

.dot-outward {
    background: #faad14;
}

.fade-enter-active,
.fade-leave-active {
    transition: opacity .28s;
}

.fade-enter-from,
.fade-leave-to {
    opacity: 0;
}

/* 弹窗美化 */
.status-dot {
    font-size: 1em;
    margin-right: 5px;
    vertical-align: middle;
}

.el-descriptions__label {
    font-weight: 500;
    letter-spacing: 1px;
}

/* 移动端适配 */
@media (max-width: 950px) {
    .bed-list-scroll {
        padding: 2vw 1vw;
    }

    .room-block-card {
        padding: 14px 8px 14px 10px;
    }

    .legend-box {
        right: 1vw;
        font-size: 0.93em;
        width: 23vw;
    }
}

@media (max-width: 600px) {
    .legend-box {
        width: 95vw;
        left: 2vw;
        right: auto;
        top: 96vh;
    }
}

.status-toggle-icon {
  width: 40px;
  height: 40px;
  border-radius: 50%;
  background: #f5f5f5;
  display: flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  box-shadow: 0 1px 4px #eee;
  transition: background 0.2s, box-shadow 0.2s;
}


.status-toggle-icon .icon {
  width: 22px;
  height: 22px;
  display: block;
}

</style>
  